<?php

/**
 * 拍拍乐组件文件
 *
 */
class ppl_plug
{
    /**
     *
     *拍拍乐组件配置文件
     **/
    //拍拍乐规则描述
    public $pplrule;
    protected $xuanzezu;
    protected $check;
    protected $content;
    protected $from;
    protected $request;
    protected $pplconf;

    public function __construct($xuanzezu, $check, $request)
    {
        $this->xuanzezu = $xuanzezu;
        $this->check = $check;
        
        $this->request = $request;
    }

    /**
     * 处理投票的消息回复
     * */
    public function pplvotereplay($from, $rule)
    {
        $reply = array(
            new NewsResponseItem('点击进入拍拍乐投票', $rule, Web_ROOT . "/paipaile/images/pplshow.jpg", Web_ROOT . "/paipaile/mobile/index.php?wecha_id={$from}")
        );
        Wechat::$noendtail = 0;
        return $reply;
    }

    /**
     * 拍拍乐回复函数
     *
     * @return void
     */
    public function ppl_replay($tpye = 'text')
    {
        $time = $this->request['createtime'];
        $content = $this->request['content'];
        $from = $this->request['fromusername'];

        if ($this->request['msgtype'] == 'image') {
            $tpye = 'img';
        }
//        $xuanzezu = $this->xuanzezu;
        $check = $this->check;
        $paipaile_m = new M('paipaile');
        //锁定回话
        if ($check['flag'] != 4) {
            $sql_check='UPDATE  `weixin_flag` SET `flag` = "4"  WHERE  `openid` = "'.$from.'";';
            $paipaile_m->query($sql_check);
            $sql1 = 'INSERT INTO `weixin_paipaile_user` (`openid`,`nickname`,`avatar`,`fromtype`,`status`) VALUES ("'.$from.'","' . $check['nickname'] . '","' . $check['avatar'] . '","' . $check['fromtype'] . '",1);';
            $paipaile_m->query($sql1);
        }
        $pplconfc = new M('paipaile_config');
        $pplconf = $pplconfc->find();
        $this->pplconf = $pplconf;
        $this->pplrule = $pplconf['rule'];
        $ppluserinfoc = new M('paipaile_user');
        $ppluserinfo = $ppluserinfoc->find('openid="'.$from.'"');
        /**
         * 处理上传图片后的消息回复
         * */
        function pplimgreplay($imgid, $from, $content)
        {
            $reply = array(
                new NewsResponseItem("您是第" . $imgid . "位上传照片的人哟！", '', $content, Web_ROOT . "/paipaile/mobile/index.php?wecha_id={$from}"),
                new NewsResponseItem("点击进入拍拍乐进行投票哦！", '', '', Web_ROOT . "/paipaile/mobile/index.php?wecha_id={$from}"),
            );

            return $reply;
        }

        if ($content == '规则') {
            $this->responseText($this->pplrule);
            die;
        }
        if ($content == '上墙') {
//            $sql_check = "UPDATE  `weixin_flag` SET `flag` = '2'  WHERE  `openid` =  '{$from}';";
            $sql_check = 'UPDATE  `weixin_flag` SET `flag` = "2"  WHERE  `openid` =  "'.$from.'";';
//            mysql_query($sql_check);
            $pplconfc->query($sql_check);
            $this->responseText('已经返回至上墙模式，您可以发送信息参与上墙互动了！');
            die;
        }
        if ($pplconf['black_switch']) {
            if ($ppluserinfo['status'] == 3) {
                $this->responseText('对不起，主人说了已经中奖的用户，不能再参与游戏了哦！');
                die;
            }
        }
        /**
         * 拍拍乐回复函数$xuanzezu[26]为拍拍乐当前状态
         * $pplconf['pplnum']=1表示未上传
         * $pplconf['pplnum']=2表示上传中
         * $pplconf['pplnum']=3表示投票中
         * *$check[2]=4表示用户当前是否处于拍拍乐模式
         * $ppluserinfo['status']表示该用户的投票状态 0表示该用户未投票
         * $ppluserinfo['status']表示该用户的投票状态 1表示该用户已经投过票
         * $ppluserinfo['status']表示该用户的投票状态 2表示该用户未投票，但已经上传过照片，不能再上传照片
         * $ppluserinfo['status']表示该用户的投票状态 3表示该用户已经中过奖，或已经上传成功过照片了
         * */
        if ($pplconf['pplnum'] == 1) {
            $reply = $this->pplstart();
        } else if ($pplconf['pplnum'] == 2) {
            if ($ppluserinfo['status'] == 2) {
                $this->responseText('您已经上传过照片了！请等待投票开始后参与投票进行互动吧！');
                die;
            }
            if ($tpye == 'img') {
                $imgid = $this->pplset($from, $time, $this->request['picurl'], $check);
                if ($imgid == "enough") {
                    $reply = $this->pplvotereplay($from, $this->pplrule);
                } else {
                    $reply = pplimgreplay($imgid, $from, $this->request['picurl']);
                }
            } else {
                $this->responseText('已经可以上传照片啦！赶快上传你的照片参与吧！
游戏规则：' . $this->pplrule);
                die;
            }
        } else if ($pplconf['pplnum'] == 3) {
            if (!$this->pplconf['must_switch'] && $tpye == 'img') {
                $imgid = $this->pplset($from, $time, $this->request['picurl'], $check);
                if ($imgid == "enough") {
                    $reply = $this->pplvotereplay($from, $this->pplrule);
                } else {
                    $reply = pplimgreplay($imgid, $from, $this->request['picurl']);
                }
            } else {
                $reply = $this->pplvotereplay($from, $this->pplrule);
            }
        }
        if (Wechat::$noendtail) {
            $end = new NewsResponseItem('回复【规则】查看游戏规则');
            $end2 = new NewsResponseItem('回复【上墙】返回上墙模式');
            array_push($reply, $end, $end2);
        }
        return $reply;
    }


    public function pplstart()
    {

        $reply = array(
            new NewsResponseItem('游戏还没开始'),
            new NewsResponseItem('请在游戏开始后再上传您的照片！'),
        );
        return $reply;
    }

    /**
     * 回复文本消息
     *
     * @param  string $content 消息内容
     * @param  integer $funcFlag 默认为0，设为1时星标刚才收到的消息
     * @return void
     */
    protected function responseText($content, $funcFlag = 0)
    {
        exit(new TextResponse($this->request['fromusername'], $this->request['tousername'], $content, $funcFlag));
    }

    /**
     *
     * 处理新用户发来的照片
     * */
    public function pplset($from, $time, $picurl, $check)
    {
        $paipaile_m = new M('paipaile');
        $result = $paipaile_m->find('1', 'max(id) mid');

        // $result = mysql_query('select max(id) from `weixin_paipaile`');
        // $row = mysql_fetch_row($result);
        // $maxid = $row[0];
        $maxid = $result['mid'];
        if ($maxid >= $this->pplconf['pplvotenum']) {
            $sql1 = "UPDATE  `weixin_paipaile_config` SET  `pplnum`='3'";
//            mysql_query($sql1);
            $paipaile_m->query($sql1);
            return "enough";
        } else {
            $sql1 = 'UPDATE  `weixin_paipaile_user` SET  `status`="2" WHERE `openid` = "'.$from.'"';
            $paipaile_m->query($sql1);
//            mysql_query($sql1);
            $nicheng = $check[4];
            $avatar = $check[5];
//            $sql1 = "INSERT INTO `weixin_paipaile` (`id`,`openid`,`nickname`,`avatar`,`picurl`,`voteres`,`fromtype`) VALUES ('','$from','$nicheng','$avatar','$picurl',0,'$check[17]')";
            $sql1 = 'INSERT INTO `weixin_paipaile` (`openid`,`nickname`,`avatar`,`picurl`,`voteres`,`fromtype`) VALUES ("'.$from.'","'.$nicheng.'","'.$avatar.'","'.$picurl.'",0,"'.$check[17].'");';
            $paipaile_m->query($sql1);
            if (!$this->pplconf['must_switch']) {
                $sql1 = "UPDATE  `weixin_paipaile_config` SET  `pplnum`='3'";
                $paipaile_m->query($sql1);
//                mysql_query($sql1);
                return "enough";
            }
            $insert_id = $paipaile_m->insert_id();
            if ($insert_id >= $this->pplconf['pplvotenum']) {
                $sql1 = "UPDATE  `weixin_paipaile_config` SET  `pplnum`='3'";
                $paipaile_m->query($sql1);
//                mysql_query($sql1);
                return "enough";
            } else {
                return $insert_id;
            }
        }
    }
}